package com.ruoyi.web.controller.store;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.CommonResult;
import com.ruoyi.store.vo.CategoryTreeVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.store.vo.HmCategoryVo;
import com.ruoyi.store.bo.HmCategoryQueryBo;
import com.ruoyi.store.bo.HmCategoryAddBo;
import com.ruoyi.store.bo.HmCategoryEditBo;
import com.ruoyi.store.service.IHmCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 商品分类Controller
 *
 * @author ruoyi
 * @date 2021-07-02
 */
@Api(value = "商品分类控制器", tags = {"商品分类管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/store/category")
public class HmCategoryController extends BaseController {

    private final IHmCategoryService iHmCategoryService;

    /**
     * 查询商品分类列表
     */
    @ApiOperation("查询商品分类列表")
    @PreAuthorize("@ss.hasPermi('store:category:list')")
    @GetMapping("/list")
    public TableDataInfo<HmCategoryVo> list(@Validated HmCategoryQueryBo bo) {
        return iHmCategoryService.queryPageList(bo);
    }

    /**
     * 导出商品分类列表
     */
    @ApiOperation("导出商品分类列表")
    @PreAuthorize("@ss.hasPermi('store:category:export')")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<HmCategoryVo> export(@Validated HmCategoryQueryBo bo) {
        List<HmCategoryVo> list = iHmCategoryService.queryList(bo);
        ExcelUtil<HmCategoryVo> util = new ExcelUtil<HmCategoryVo>(HmCategoryVo.class);
        return util.exportExcel(list, "商品分类");
    }

    /**
     * 获取商品分类详细信息
     */
    @ApiOperation("获取商品分类详细信息")
    @PreAuthorize("@ss.hasPermi('store:category:query')")
    @GetMapping("/{id}")
    public AjaxResult<HmCategoryVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Integer id) {
        return AjaxResult.success(iHmCategoryService.queryById(id));
    }

    /**
     * 新增商品分类
     */
    @ApiOperation("新增商品分类")
    @PreAuthorize("@ss.hasPermi('store:category:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody HmCategoryAddBo bo) {
        return toAjax(iHmCategoryService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改商品分类
     */
    @ApiOperation("修改商品分类")
    @PreAuthorize("@ss.hasPermi('store:category:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody HmCategoryEditBo bo) {
        return toAjax(iHmCategoryService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除商品分类
     */
    @ApiOperation("删除商品分类")
    @PreAuthorize("@ss.hasPermi('store:category:remove')")
    @Log(title = "商品分类" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Integer[] ids) {
        return toAjax(iHmCategoryService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

	/**
	 * 查询分类表信息
	 * @author Mr.Zhang
	 * @since 2020-04-16
	 */
	@ApiOperation(value = "获取tree结构的列表")
	@RequestMapping(value = "/list/tree", method = RequestMethod.GET)
	@ApiImplicitParams({
		@ApiImplicitParam(name="type", value="类型ID | 类型，1 产品分类，2 附件分类，3 文章分类， 4 设置分类， 5 菜单分类， 6 配置分类， 7 秒杀配置", example = "1"),
		@ApiImplicitParam(name="status", value="-1=全部，0=未生效，1=已生效", example = "1"),
		@ApiImplicitParam(name="name", value="模糊搜索", example = "电视")
	})
	public CommonResult<List<CategoryTreeVo>> getListTree(@RequestParam(name = "type") Integer type,
														  @RequestParam(name = "status") Integer status,
														  @RequestParam(name = "name", required = false) String name){
		List<CategoryTreeVo> listTree = iHmCategoryService.getListTree(type,status,name);
		return CommonResult.success(listTree);
	}
}
